/**
 * Created by yananliu on 2018/4/17.
 */
export class UrlUtil{

    static readURLParam (name,url) {
        if(url && url.indexOf('?') === -1) return null;
        if(window.location.search === '') return null;
        const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        let r = null;
        if(!url){
            r = window.location.search.substr(1).match(reg);  //匹配目标参数
        }else{
            r = url.split('?')[1].match(reg);
        }
        if (r !== null) return decodeURI(r[2]);
        return null; //返回参数值
    };
    /**
     * 更新url 上的参数
     *
     * */
    static updateURLParameter (url, paramName, paramValue){
        if (paramValue === null) {
            paramValue = '';
        }
        let pattern = new RegExp('\\b('+paramName+'=).*?(&|$)');
        if (url.search(pattern)>=0) {
            return url.replace(pattern,'$1' + paramValue + '$2');
        }
        url = url.replace(/\?$/,'');
        return url + (url.indexOf('?')>0 ? '&' : '?') + paramName + '=' + paramValue;
    };

}